﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Gui.Commands;
using Kinect.Toolbox.Recording;

namespace Gui.ViewModels
{
    public class RecorderViewModel : INotifyPropertyChanged, IDisposable
    {
        private bool _actButtonEnabled;
        private bool _stopButtonEnabled;
        private string _recordLabel;

        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand RecordButtonClickCommand { get; set; }
        public ICommand PlayButtonClickCommand { get; set; }
        public ICommand StopCommand { get; private set; }

        public bool ActButtonEnabled
        {
            get { return _actButtonEnabled; }
            set
            {
                _actButtonEnabled = value;
                OnPropertyChanged("ActButtonEnabled");
            }
        }

        public bool StopButtonEnabled
        {
            get { return _stopButtonEnabled; }
            set
            {
                _stopButtonEnabled = value;
                OnPropertyChanged("StopButtonEnabled");
            }
        }

        public string RecordLabel
        {
            get { return _recordLabel; }
            set
            {
                _recordLabel = value;
                OnPropertyChanged("RecordLabel");
            }
        }

        public FramePlayer FramePlayer { get; set; }

        public FrameRecorder FrameRecorder { get; set; }

        public IStartableStoppable StartedDevice { get; set; }


        public RecorderViewModel()
        {
            FramePlayer = new FramePlayer();
            FrameRecorder = new FrameRecorder();
            FramePlayer.Stopped += StartableStoppableOnStopped;
            FramePlayer.Started += StartableStoppableOnStarted;
            FrameRecorder.Stopped += StartableStoppableOnStopped;
            FrameRecorder.Started += StartableStoppableOnStarted;

            RecordButtonClickCommand = new RelayCommand<FrameRecorder>(StartRecording);
            PlayButtonClickCommand = new RelayCommand<FramePlayer>(StartPlaying);
            StopCommand = new RelayCommand<IStartableStoppable>(Stop);

            RecordLabel = "Record";
            ActButtonEnabled = true;
        }

        private void StartPlaying(FramePlayer obj)
        {
            Task.Run(() =>
            {
                obj.Start();
                StartedDevice = obj;
            });
        }

        private void StartRecording(FrameRecorder obj)
        {
            Task.Run(() =>
            {
                CountdownRecordLabel();
                obj.Start();
                StartedDevice = obj;
            });
        }

        private void CountdownRecordLabel()
        {
            RecordLabel = "--3--";
            Thread.Sleep(1000);
            RecordLabel = "--2--";
            Thread.Sleep(1000);
            RecordLabel = "--1--";
            Thread.Sleep(1000);
        }

        private void Stop(IStartableStoppable startableStoppable)
        {
            StartedDevice.Stop();
            StartedDevice = null;
        }


        private void StartableStoppableOnStarted(object sender, EventArgs eventArgs)
        {
            ActButtonEnabled = false;
            StopButtonEnabled = true;
            RecordLabel = "Record";
        }

        private void StartableStoppableOnStopped(object sender, EventArgs eventArgs)
        {
            StopButtonEnabled = false;
            ActButtonEnabled = true;
        }

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public void Dispose()
        {
            FramePlayer.Stopped -= StartableStoppableOnStopped;
            FramePlayer.Started -= StartableStoppableOnStarted;
            FrameRecorder.Stopped -= StartableStoppableOnStopped;
            FrameRecorder.Started -= StartableStoppableOnStarted;
        }
    }
}
